<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @author 		Efre Team
 * @package 	PyroCMS
 * @subpackage 	Pro Module
 * @category 	Modules
 * @license 	Apache License v2.0
 */
class Content extends Public_Controller
{
	/**
	 * Constructor method
	 *
	 * @author PyroCMS Dev Team
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->library('module_helper');
		$this->load->library('email');
		$this->load->helper('email');

		$config['protocol'] = 'mail';
	//	$config['mailpath'] = '/usr/sbin/sendmail';
		$config['charset'] = 'utf-8';
	//	$config['mailtype']	= 'html';

		$this->email->initialize($config);
	}

	/**
	 * Index method
	 *
	 * @access public
	 * @return void
	 */
	public function index()
	{
		redirect(site_url());
	}

	function contact_us(){
		$this->data = null;

		$this->template
			->title('お問い合わせ')
			->build('contact_us', $this->data);
	}

	function contact_send(){
		$data = $_POST['data'];

		$data_array = array();
		$field = array(	'name_field'=>'contact.form.name',
						'email_field'=>'contact.form.email',
						'email_field2'=>'contact.form.email2',
						'aaa'=>'contact.form.AAA','bbb'=>'contact.form.BBB',
						'ccc'=>'contact.form.CCC','ddd'=>'contact.form.DDD',
						'eee'=>'contact.form.EEE','fff'=>'contact.form.FFF',
						'inquiry'=>'contact.form.inquiry');

		foreach ($data as $array){
			$data_array[$array['name']] = mysql_real_escape_string($array['value']);
		}

		$from_email = $this->module_helper->getSetting( 'server_email' );// from system
		$to_email = $this->module_helper->getSetting( 'contact_email' );// to admin

	//echo $from_email.'-'.$to_email;exit();

		$subject = lang('newContactEmailToAdmin');

		$content = "";
		foreach($field as $key=>$val){
			if(isset($data_array[$key])){
				$content .= "<b>".lang($val)."</b>:".$data_array[$key]."<br/>";
			}
		}
	//	$this->email->subject($subject);
	//	$this->email->message( $content );

		// SEND MAIL. DO NOT ACTION UNDER LOCALHOST
		if( ENVIRONMENT != 'local' ){
			//$this->email->send();
			if( $this->send($from_email, $to_email, $subject, $content) ) {
				echo lang('contact_send_ok');
				exit();
			}else{
				echo 'Oop. The email not send';exit();
			}
		}else{

		}
		echo lang('contact_send_ok');
		exit();
	}

	function send($from, $to, $subject, $content){
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

		$headers .= "To: $to" . "\r\n";
		$headers .= "From: $from" . "\r\n";
		return mail($to, $subject, $content, $headers);
	}

}